package com.lechuang.common.result;

import com.lechuang.common.context.ThreadLocalContext;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * <p>
 *  统一返回对象
 * </p>
 * @author liaoningbo
 * @since 2023/5/31
 */
@Data
@NoArgsConstructor
@Accessors(chain = true)
public class Result<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;
    private String traceId;


    public static <T> Result<T> success(T data){
        return new Result<T>()
                .setCode(ResultEnum.SUCCEED.getCode())
                .setMessage(ResultEnum.SUCCEED.getMessage())
                .setData(data)
                .setTraceId(ThreadLocalContext.traceThreadLocal.get());
    }

    public static <T> Result<T> success(){
        return new Result<T>()
                .setCode(ResultEnum.SUCCEED.getCode())
                .setMessage(ResultEnum.SUCCEED.getMessage())
                .setTraceId(ThreadLocalContext.traceThreadLocal.get());
    }

    public static <T> Result<T> error(){
        return new Result<T>()
                .setCode(ResultEnum.ERROR.getCode())
                .setMessage(ResultEnum.ERROR.getMessage())
                .setTraceId(ThreadLocalContext.traceThreadLocal.get());
    }

    public static <T> Result<T> error(String message){
        return new Result<T>()
                .setCode(ResultEnum.ERROR.getCode())
                .setMessage(message)
                .setTraceId(ThreadLocalContext.traceThreadLocal.get());
    }

    public static <T> Result<T> error(Integer code, String message){
        return new Result<T>()
                .setCode(code)
                .setMessage(message)
                .setTraceId(ThreadLocalContext.traceThreadLocal.get());
    }

    public static <T> Result<T>  result(ResultEnum resultEnum){
        return new Result<T>()
                .setCode(resultEnum.getCode())
                .setMessage(resultEnum.getMessage())
                .setTraceId(ThreadLocalContext.traceThreadLocal.get());
    }
}
